package me.jayramafisher.mcsurvival;

import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.Location;
import org.jnbt.*;
import java.util.TreeMap;

/**
 *The MarketManager class handles all market storage and operation.
 */

public class MarketManager
{
    SurvivalPlugin parent;
    CompoundTag market;

    public MarketManager(SurvivalPlugin nparent)
    {
        parent = nparent;
        try {
            File file = new File("plugins/MCSurvival/markets.nbt");
            if(file.exist()) {
                market = NBTManager.load(file);
            }
            else market = new CompoundTag("market",new TreeMap<String,Tag>());
        }
        catch(Exception e)
        {
            market = null;
        }
        if(market == null) parent.log.println("Error: Failed to load markets.nbt.");
        else parent.log.println("Successfully initialized MarketManager.");
    }

    public void onDisable() {
        try {
            File file = new File("plugins/MCSurvival/markets.nbt");
            NBTManager.save(file,market);
        }
        catch (Exception e)
        {
            markets = null;
        }
        if(market == null) parent.log.println("Error: Failed to save markets.nbt.");
        else parent.log.println("Successfully saved markets.nbt");
    }

    public boolean addSign(SignChangeEvent evt)
    {
        //signs.add(evt.getBlock().getLocation());
        //TODO: define the market.nbt structure and add the sign to it here
    }

}
